Fix 5 SonarQube issues including cognitive complexity and method length#1741
Open
sonarqube-agent[bot] wants to merge 1 commit into
Open
Fix 5 SonarQube issues including cognitive complexity and method length#1741sonarqube-agent[bot] wants to merge 1 commit into
sonarqube-agent[bot] wants to merge 1 commit into
Conversation
Fixed issues: - AZkoVAVQIsbR56mqmhNH for java:S2629 rule - AZkz3mbD-GVAOO9Z9Iiy for java:S3824 rule - AZ7dogobU_MVOKqD8-MV for java:S8786 rule - AZkoU-8TIsbR56mqmhGP for java:S3776 rule - AZkz3mdx-GVAOO9Z9Ii0 for java:S138 rule Generated by SonarQube Agent (task: efb36fad-c3d8-4b80-ac8b-5a3d37f5a8b6)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolved a critical cognitive complexity issue by extracting complex logic into helper methods, split an oversized 193-line method into focused smaller methods, optimized a regex pattern vulnerable to super-linear backtracking, improved conditional logging to avoid unnecessary method invocations, and modernized Map usage with computeIfAbsent patterns. These changes enhance code maintainability, performance, and adherence to SonarQube quality standards.
View Project in SonarCloud
Fixed Issues
java:S3824 - Replace this "Map.containsKey()" with a call to "Map.computeIfAbsent()". • MAJOR • View issue
Location:
php:php-checks/src/main/java/org/sonar/php/checks/phpunit/NoAssertionInTestCheck.java:130Why is this an issue?
It’s a common pattern to test the result of a
java.util.Map.get()againstnullor callingjava.util.Map.containsKey()before proceeding with adding or changing the value in the map. However thejava.util.MapAPI offers a significantly better alternative in the form of thecomputeIfPresent()andcomputeIfAbsent()methods. Using these instead leads to cleaner and more readable code.What changed
This hunk fixes the code smell where
Map.containsKey()was used followed byMap.put()with the same key, instead of using the more concise and readableMap.computeIfAbsent()or similar API. The original code checked!assertionInMethod.containsKey(methodDeclaration)and then calledassertionInMethod.put(methodDeclaration, false)inside the if-block. The fix replaces this pattern withassertionInMethod.putIfAbsent(methodDeclaration, false) == null, which atomically checks for the key's absence and inserts the value in a single call, making the code cleaner and more idiomatic.java:S138 - This method has 193 lines, which is greater than the 100 lines authorized. Split it into smaller methods. • MAJOR • View issue
Location:
php:php-checks/src/main/java/org/sonar/php/checks/utils/PhpUnitCheck.java:39Why is this an issue?
A method that grows too large tends to aggregate too many responsibilities. Such method inevitably become harder to understand and therefore harder to maintain.
What changed
This hunk replaces the beginning of the overly long
assertions()method by delegating to four smaller helper methods (arrayContainsAndEqualityAssertions(),fileAndDirectoryAssertions(),typeStateAndIdentityAssertions(),stringXmlJsonAndConstraintAssertions()), each returning aStream<Assertion>. This splits the original 193-line method into smaller, focused methods, bringing the line count ofassertions()well below the 100-line threshold and resolving the code smell about methods that are too large.java:S2629 - Invoke method(s) only conditionally. • MAJOR • View issue
Location:
php:sonar-php-plugin/src/main/java/org/sonar/plugins/php/reports/phpunit/PhpUnitReportImporter.java:38Why is this an issue?
Some method calls can effectively be "no-ops", meaning that the invoked method does nothing, based on the application’s configuration (eg: debug logs in production). However, even if the method effectively does nothing, its arguments may still need to evaluated before the method is called.
What changed
This hunk fixes the code smell where methods like
reportName()andreportPathKey()are invoked unconditionally as arguments to a logging call. By adding&& logger().isInfoEnabled()to the condition, the code ensures that the subsequent logging statement (which callsreportName()andreportPathKey()) is only reached when the logger's info level is actually enabled. This prevents the unnecessary evaluation of those method calls when the log level is too high to display the message, addressing the performance penalty of invoking methods only to pass their results to a no-op logging call.java:S3776 - Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed. • CRITICAL • View issue
Location:
php:php-checks/src/main/java/org/sonar/php/checks/DeadStoreCheck.java:94Why is this an issue?
Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.
What changed
This hunk replaces the inline logic for processing symbol usages (which included multiple nested if statements, conditions with logical operators, and loop nesting) with a single method call to
processSymbolUsage. By extracting this code into a separate method, the cognitive complexity of theverifyBlockmethod is reduced because the nested conditionals and their associated complexity increments are moved out of the method. This directly addresses the code smell where theverifyBlockmethod's cognitive complexity exceeded the allowed threshold of 15.java:S8786 - Simplify this regular expression to reduce its runtime, as it has super-linear performance due to backtracking. • MAJOR • View issue
Location:
php:php-checks/src/main/java/org/sonar/php/checks/phpunit/NoAssertionInTestCheck.java:46Why is this an issue?
Regular expression engines use backtracking to try all possible execution paths when evaluating a pattern against an input. In some cases, this leads to non-linear backtracking where the worst-case evaluation time grows polynomially (e.g., O(n²) or O(n³)) with the input size. While not as severe as catastrophic backtracking, such patterns can significantly degrade application performance when processing large or untrusted inputs.
What changed
This hunk fixes the regular expression that exhibited super-linear (polynomial) backtracking performance. The original pattern
(assert|verify|fail|pass|should|will|check|expect|validate|.*test).*contained.*testinside a group followed by.*, which caused the regex engine to backtrack excessively on non-matching inputs. The fix restructures the pattern to(assert|verify|fail|pass|should|will|check|expect|validate).*|.*test.*, moving the.*test.*alternative outside the group. This eliminates the ambiguous overlap between.*testand the trailing.*quantifier, reducing the backtracking complexity from super-linear to linear.SonarQube Remediation Agent uses AI. Check for mistakes.